import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/afterSales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详情'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改'
});
const publishDesc = formatMessage({
    id: 'api.message.publishDesc',
    defaultMessage: '发布'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废'
});
const partQueryDesc = formatMessage({
    id: 'api.message.partQueryDesc',
    defaultMessage: '查询备件信息'
});
const getProductListDesc = formatMessage({
    id: 'api.message.getProductListDesc',
    defaultMessage: '查询产品'
});
const getVehiclesDesc = formatMessage({
    id: 'api.message.getVehiclesDesc',
    defaultMessage: '查询车辆档案信息'
});
const queryDealerDesc = formatMessage({
    id: 'api.message.queryDealerDesc',
    defaultMessage: '查询经销商'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/serviceActivitie/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/serviceActivities?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/serviceActivities/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const addApplyReport = data => request(`${baseUrl}/serviceActivities`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));
//修改
export const updateApplyReport = (id, data) => request(`${baseUrl}/serviceActivities/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 发布
export const publishData = (id, data) => request(`${baseUrl}/serviceActivities/${id}/publish`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, publishDesc));
// 作废
export const abandonData = (id, data) => request(`${baseUrl}/serviceActivities/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
//服务站备件查询
export const partQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partQueryDesc));
};
// 查询车型
export const getProduct = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/products?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getProductListDesc));
};

// 查询车辆档案信息
export const getVehicles = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/vehicleSolds/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getVehiclesDesc));
};
// 查询经销商
export const getDealers = str => {
    const queryString = stringify(str);
    return request(`${baseUrl}/dealers/forClaim?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, queryDealerDesc));
};

// 查询产品分类
export const getProductCategories = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, '查询产品分类'));
};
